package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.order.bo.*;
import com.cskaoyan.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 创建日期: 2022/05/06 14:34
 *
 * @author JinLu Zhou
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @GetMapping("list")
    public BaseRespVo list(@Valid OrderBo orderBo, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            // 拿到成员变量的错误
            FieldError fieldError = bindingResult.getFieldError();
            // 不合法的值是什么
            Object rejectedValue = fieldError.getRejectedValue();
            // 成员变量名 → 请求参数名
            String field = fieldError.getField();
            // 默认的消息
            String defaultMessage = fieldError.getDefaultMessage();
            String msg = "请求参数" + field + "因为传入了值" + rejectedValue + "没有通过校验：" + defaultMessage;
            return BaseRespVo.invalidParameter(msg);
        }
        return orderService.selectAllOrders(orderBo);
    }

    @GetMapping("detail")
    public BaseRespVo detail(@Valid IdBo id, BindingResult bindingResult) {

        if (bindingResult.hasFieldErrors()) {
            // 拿到成员变量的错误
            FieldError fieldError = bindingResult.getFieldError();
            // 不合法的值是什么
            Object rejectedValue = fieldError.getRejectedValue();
            // 成员变量名 → 请求参数名
            String field = fieldError.getField();
            // 默认的消息
            String defaultMessage = fieldError.getDefaultMessage();
            String msg = "请求参数" + field + "因为传入了值" + rejectedValue + "没有通过校验：" + defaultMessage;
            return BaseRespVo.invalidParameter(msg);
        }
        return orderService.selectOrdersDetail(id.getId());
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        return orderService.deleteOrderById(orderId);
    }

    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody ShipBo shipBo) {
        return orderService.ship(shipBo);
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody RefundBo refundBo) {
        return orderService.refund(refundBo);
    }

    @RequestMapping("channel")
    public BaseRespVo channel() {
        return orderService.channel();
    }

    @RequestMapping("reply")
    public BaseRespVo reply(@RequestBody CommentBo commentBo) {
        return orderService.reply(commentBo);
    }
}
